﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Yuebon.Commons;
using Yuebon.Commons.IoC;
using Yuebon.Commons.Models;
using Yuebon.Shop.Dtos;
using Yuebon.Shop.IServices;
using Yuebon.Shop.Models;

namespace Yuebon.Shop.Application
{
    public class ReferralsApp
    {
        IReferralsService service = IoCContainer.Resolve<IReferralsService>();
        IReferralGradesService serviceReferralGradesService = IoCContainer.Resolve<IReferralGradesService>();
        IMembersService serviceMembers = IoCContainer.Resolve<IMembersService>();

        /// <summary>
        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
        /// </summary>
        /// <param name="condition">查询的条件</param>
        /// <param name="info">分页实体</param>
        /// <param name="fieldToSort">排序字段</param>
        /// <param name="desc">是否降序</param>
        /// <returns>指定对象的集合</returns>
        public List<ReferralsDto> FindWithPager(string where, PagerInfo pagerInfo, string fieldToSort, bool desc)
        {
            List<Referrals> list = service.FindWithPager(where, pagerInfo, fieldToSort, desc);
            List<ReferralsDto> resultList = new List<ReferralsDto>();
            foreach (Referrals item in list)
            {
                ReferralsDto referralsDto = new ReferralsDto();
                referralsDto.Id = item.Id;

                referralsDto.MemberId = item.MemberId;
                if (!string.IsNullOrEmpty(item.MemberId))
                {
                    referralsDto.MemberName = serviceMembers.Get(item.MemberId).UserName;
                }
                referralsDto.GradeId = item.GradeId;
                referralsDto.GradeName = serviceReferralGradesService.Get(item.GradeId).Name;
                referralsDto.Email = item.Email;
                referralsDto.Address = item.Adress;
                referralsDto.AuditDate = item.AuditDate;
                referralsDto.BannerUrl = item.BannerUrl;
                referralsDto.Cellphone = item.Cellphone;
                referralsDto.Email = item.Email;
                referralsDto.IsRepeled = item.IsRepeled;
                referralsDto.ReferralStatus = item.ReferralStatus;
                referralsDto.RefusalReason = item.RefusalReason;
                referralsDto.RepelReason = item.RepelReason;
                referralsDto.RepelTime = item.RepelTime;
                referralsDto.RequetDate = item.RequetDate;
                referralsDto.RequetReason = item.RequetReason;
                referralsDto.ShopName = item.ShopName;
                referralsDto.ReferralsURL = item.ReferralsURL;
                resultList.Add(referralsDto);
            }
            return resultList;
        }

        public List<ReferralsDto> GetAll() {
            IEnumerable<Referrals> list = service.GetListWhere("IsRepeled=0");
            List<ReferralsDto> resultList = new List<ReferralsDto>();
            foreach (Referrals item in list)
            {
                ReferralsDto referralsDto = new ReferralsDto();
                referralsDto.Id = item.Id;

                referralsDto.MemberId = item.MemberId;
                if (!string.IsNullOrEmpty(item.MemberId))
                {
                    referralsDto.MemberName = serviceMembers.Get(item.MemberId).UserName;
                }
                referralsDto.GradeId = item.GradeId;
                referralsDto.GradeName = serviceReferralGradesService.Get(item.GradeId).Name;
                referralsDto.Email = item.Email;
                referralsDto.Address = item.Adress;
                referralsDto.AuditDate = item.AuditDate;
                referralsDto.BannerUrl = item.BannerUrl;
                referralsDto.Cellphone = item.Cellphone;
                referralsDto.Email = item.Email;
                referralsDto.IsRepeled = item.IsRepeled;
                referralsDto.ReferralStatus = item.ReferralStatus;
                referralsDto.RefusalReason = item.RefusalReason;
                referralsDto.RepelReason = item.RepelReason;
                referralsDto.RepelTime = item.RepelTime;
                referralsDto.RequetDate = item.RequetDate;
                referralsDto.RequetReason = item.RequetReason;
                referralsDto.ShopName = item.ShopName;
                referralsDto.ReferralsURL = item.ReferralsURL;
                resultList.Add(referralsDto);
            }
            return resultList;
        }

        /// <summary>
        /// 根据分销员ID获取信息
        /// </summary>
        /// <param name="id">分销员ID</param>
        /// <returns></returns>
        public ReferralsDto GetById(string id)
        {
            Referrals referrals = service.Get(id);
            ReferralsDto referralsDto = new ReferralsDto();
            if (referrals != null)
            {
                referralsDto.Id = referrals.Id;

                referralsDto.MemberId = referrals.MemberId;
                if (!string.IsNullOrEmpty(referrals.MemberId))
                {
                    Members members = serviceMembers.Get(referrals.MemberId);
                    referralsDto.MemberName = members.RealName==""? members.UserName: members.RealName;
                }
                referralsDto.GradeId = referrals.GradeId;
                referralsDto.GradeName = serviceReferralGradesService.Get(referrals.GradeId).Name;
                referralsDto.Email = referrals.Email;
                referralsDto.Address = referrals.Adress;
                referralsDto.AuditDate = referrals.AuditDate;
                referralsDto.BannerUrl =referrals.BannerUrl;
                referralsDto.Cellphone = referrals.Cellphone;
                referralsDto.Email = referrals.Email;
                referralsDto.IsRepeled = referrals.IsRepeled;
                referralsDto.ReferralStatus = referrals.ReferralStatus;
                referralsDto.RefusalReason = referrals.RefusalReason;
                referralsDto.RepelReason = referrals.RepelReason;
                referralsDto.RepelTime = referrals.RepelTime;
                referralsDto.RequetDate = referrals.RequetDate;
                referralsDto.RequetReason = referrals.RequetReason;
                referralsDto.ShopName = referrals.ShopName;
                referralsDto.ReferralsURL = referrals.ReferralsURL;
            }
            return referralsDto;
        }

        /// <summary>
        /// 根据分销员ID获取信息
        /// </summary>
        /// <param name="id">分销员ID</param>
        /// <returns></returns>
        public async Task<ReferralsDto> GetByIdAsync(string id)
        {
            Referrals referrals =await service.GetAsync(id);
            ReferralsDto referralsDto = new ReferralsDto();
            if (referrals != null)
            {
                referralsDto.Id = referrals.Id;

                referralsDto.MemberId = referrals.MemberId;
                if (!string.IsNullOrEmpty(referrals.MemberId))
                {
                    Members members = serviceMembers.Get(referrals.MemberId);
                    referralsDto.MemberName = members.RealName == "" ? members.UserName : members.RealName;
                }
                referralsDto.GradeId = referrals.GradeId;
                referralsDto.GradeName = serviceReferralGradesService.Get(referrals.GradeId).Name;
                referralsDto.Email = referrals.Email;
                referralsDto.Address = referrals.Adress;
                referralsDto.AuditDate = referrals.AuditDate;
                referralsDto.BannerUrl = referrals.BannerUrl;
                referralsDto.Cellphone = referrals.Cellphone;
                referralsDto.Email = referrals.Email;
                referralsDto.IsRepeled = referrals.IsRepeled;
                referralsDto.ReferralStatus = referrals.ReferralStatus;
                referralsDto.RefusalReason = referrals.RefusalReason;
                referralsDto.RepelReason = referrals.RepelReason;
                referralsDto.RepelTime = referrals.RepelTime;
                referralsDto.RequetDate = referrals.RequetDate;
                referralsDto.RequetReason = referrals.RequetReason;
                referralsDto.ShopName = referrals.ShopName;
                referralsDto.ReferralsURL = referrals.ReferralsURL;
            }
            return referralsDto;
        }

        /// <summary>
        /// 根据用户id查询是否是分销员
        /// </summary>
        public int GetWhere(string memberid) {
            Referrals referrals = service.GetWhere(string.Format("MemberId='{0}'", memberid));
            if (referrals != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 根据分销员等级查询分销员信息
        /// </summary>
        public int GetWhereByGraders(string Graders)
        {
            Referrals referrals = service.GetWhere(string.Format("GradeId='{0}'", Graders));
            if (referrals != null)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
